﻿using SmartSpace.Documents.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.Documents.UI.Parts
{
    public partial class FolderList : SmartSpace.Web.SsPart
    {
        String folder_Id = "";

        public FolderList()
            : base(new Guid("{291AC799-18E4-4EE1-831A-4258B107345E}"))
        {
        }

        protected override void OnLoad(object sender,EventArgs e)
        {
            this.RegisterCss("common", "../Resource/JavaScript/common.js");
            this.RegisterJs("site", "../Resource/Style/site.css");

            DocServices services = new DocServices();
            DataTable dt = services.GetAllFolders();
            if (!IsPostBack)
            {
                Bind_Tv(dt, folder_tree.Nodes, "0", "Id", "ParentId", "FolderName");
            }
        }

        protected void btn_delete_Click(object sender, EventArgs e)
        {
            if (folder_Id=="")
            {
                return;
            }
            Guid id = new Guid(folder_Id);

            DocServices service = new DocServices();
            service.DeleteFolder(id);
        }

        private void Bind_Tv(DataTable dt, TreeNodeCollection tnc, string pid_val, string id, string pid, string text)
        {
            string filter = string.Format("{0}={1}", pid, pid_val);
            DataRow[] rows = dt.Select(filter);

            TreeNode tn;                                            //建立TreeView的节点（TreeNode），以便将取出的数据添加到节点中

            foreach (DataRow drv in rows)
            {
                tn = new TreeNode();                                //建立一个新节点
                tn.Value = drv[id].ToString();                      //节点的Value值，一般为数据库的id值
                tn.Text = drv[text].ToString();                     //节点的Text，节点的文本显示
                tn.ImageUrl = "img.png";
                tn.NavigateUrl = "?id=" + drv[id].ToString();
                tnc.Add(tn);                                        //将该节点加入到TreeNodeCollection（节点集合）中
                Bind_Tv(dt, tn.ChildNodes, tn.Value, id, pid, text);//递归（反复调用这个方法，直到把数据取完为止）
            }
        }

        protected void btn_addFolder_Click(object sender, EventArgs e)
        {
            String folName = txt_folder_add.Text.Trim();
            String description = txt_folder_description.Text.Trim();
            String code = txt_folder_Code.Text.Trim();
            String pid = folder_Id;
            Guid id = new Guid(Guid.NewGuid().ToString("D"));

            Folder fol = new Folder();
            fol.Id = id;
            //为空的时候可能引发异常
            fol.ParentId = new Guid(pid);
            fol.FolderName = folName;
            fol.Description = description;
            fol.Code = code;

            DocServices service = new DocServices();
            service.SaveFolder(fol);
        }

        protected void folder_tree_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode node = folder_tree.SelectedNode;
            if (node == null)
            {
                folder_Id = "";
            }
            else
            {
                folder_Id = node.Value;
            }
            fid.Value = node.Value;
        }

        protected void btn_reName_Click(object sender, EventArgs e)
        {
            DocServices services = new DocServices ();
            Guid fid = new Guid("btn_reName");
            Folder folder = services.GetFolderById(fid);
            txt_old_name.Text = folder.FolderName;
            folder.FolderName = txt_new_name.Text.Trim();
            services.SaveFolder(folder);
        }
    }
}